﻿using PolePositionMobile.Entities;
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PolePositionMobile
{
    /// <summary>
    /// A form that manages a visit's general data
    /// </summary>
    public partial class frmDataCollectionStructure : frmBase
    {
        //Created by: VMCoder
        //Creation date: -
        //Last updated by: VMCoder
        //Last update: 2008.04.10
        #region Private Members
        private EVisit m_Visit;
        private List<Int32> m_TypeIdList;
        private List<Int32> m_TopIdList;
        #endregion
        #region Public Properties
        public EVisit Visit
        {
            get { return m_Visit; }
            set { m_Visit = value; }
        }
        #endregion

        public frmDataCollectionStructure()
        {
            InitializeComponent();
        }
        protected void frmDataCollectionStructure_Load(object sender, EventArgs e)
        {
            #region Config form
            lblTitle.Text = "PPI | Transmission Locating";
            lblLButtonText.Text = "Menu";
            lblRButtonText.Text = "Next";
            lblLButton.ContextMenu = menLButton;
            LButtonMenuXOffset = 1;
            LButtonMenuYOffset = 116;
            tbcTabs.SelectedIndex = 0;
            tbcTabs.Visible = true;
            tbcTabs.Enabled = true;
            PolesNumber = m_Visit.Data != null && m_Visit.Data.Poles != null ? m_Visit.Data.Poles.Length : 0;
            PhotosNumber = m_Visit.Images != null ? m_Visit.Images.Length : 0;
            #endregion
            LoadCombos();
            if (m_Visit == null)
                m_Visit = new EVisit();
            if (m_Visit.VisitTypeFk != Int32.MinValue)
                ddlType.SelectedIndex = m_TypeIdList.IndexOf(m_Visit.VisitTypeFk);
            else
                ddlType.SelectedIndex = m_TypeIdList.IndexOf(PPIConstants.K_DefaultVisitType);
            if (m_Visit.TopTypeFk != Int32.MinValue)
                ddlTop.SelectedIndex = m_TopIdList.IndexOf(m_Visit.TopTypeFk);
            else
                ddlTop.SelectedIndex = 0;
        }
        private void LoadCombos()
        {
            m_TypeIdList = Utils.FillLookupCombo(ddlType, "VisitTypes");
            m_TopIdList = Utils.FillTopsCombo(ddlTop, ddlType.SelectedIndex > -1 ? m_TypeIdList[ddlType.SelectedIndex] : 0, true);
            ddlType.SelectedIndexChanged += new System.EventHandler(ddlType_SelectedIndexChanged);
        }
        protected override void lblRButton_Click(object sender, EventArgs e)
        {
            if (ddlType.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a visit type", PPIConstants.K_AppName);
                tbcTabs.SelectedIndex = 0;
            }
            else
            {
                ShowLoading();
                Application.DoEvents();
                m_Visit.VisitTypeFk = m_TypeIdList[ddlType.SelectedIndex];
                m_Visit.TopTypeFk = m_TopIdList[ddlTop.SelectedIndex];
                if (m_Visit.Data != null && m_Visit.Data.Poles != null)
                    foreach (EPole _Pole in m_Visit.Data.Poles)
                        _Pole.TopTypeFk = m_Visit.TopTypeFk;
                frmDataCollectionPole _Form = new frmDataCollectionPole();
                _Form.Visit = m_Visit;
                _Form.TakePhotos = !chkPhotos.Checked;
                _Form.Settings = this.Settings;
                _Form.Show();
                this.Hide();
            }
        }

        private void mitCancel_Click(object sender, EventArgs e)
        {
            ShowLoading();
            Application.DoEvents();
            frmHomePage _Form = new frmHomePage();
            _Form.Settings = this.Settings;
            _Form.Show();
            this.Hide();
        }
        protected override void tabPoles_Click(object sender, EventArgs e)
        {
            lblRButton_Click(sender, e);
        }
        protected override void tabPhotos_Click(object sender, EventArgs e)
        {
            string _ErrMsg = "";
            if (ddlType.SelectedIndex == -1)
                _ErrMsg += "Please select a visit type\n";
            if (chkPhotos.Checked)
                _ErrMsg += "Cannot access the Photos form in 'No photos' mode";
            else if (m_Visit.Data == null || m_Visit.Data.Poles == null || m_Visit.Data.Poles.Length < 1)
                _ErrMsg = "There must be at least one pole in a visit";
            if (_ErrMsg != "")
            {
                MessageBox.Show(_ErrMsg, PPIConstants.K_AppName);
                tbcTabs.SelectedIndex = 0;
            }
            else
            {
                ShowLoading();
                Application.DoEvents();
                m_Visit.VisitTypeFk = m_TypeIdList[ddlType.SelectedIndex];
                frmPhotos _Form = new frmPhotos();
                _Form.Visit = m_Visit;
                _Form.Settings = this.Settings;
                _Form.Show();
            }
        }
        private void cmdResetSolution_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to reset the solution? (Can't be undone)", PPIConstants.K_AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                ResetSolution(m_Visit);
        }

        private void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_TopIdList = Utils.FillTopsCombo(ddlTop, ddlType.SelectedIndex > -1 ? m_TypeIdList[ddlType.SelectedIndex] : 0, true);
        }
    }
}
